package com.rxkj.fxx.face;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.fxx.face.http.http.HttpUtils;
import com.fxx.face.http.inteface.FaceRequestCallback;
import com.fxx.face.http.utils.MyFaceUtils;
import com.fxx.face.http.utils.SharedPreferencesUtil;
import com.rxkj.fxx.face.base.BaseActivity;


public class MainActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    private EditText etFaceId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etFaceId = findViewById(R.id.face_id);
        Button register = findViewById(R.id.register);
        Button check = findViewById(R.id.check);
        Button delete = findViewById(R.id.delete);
        Button update = findViewById(R.id.update);
        register.setOnClickListener(this);
        check.setOnClickListener(this);
        delete.setOnClickListener(this);
        update.setOnClickListener(this);
        mContext = this;
        MyFaceUtils.initLicense(mContext);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register:
                goActivity(1);
                break;
            case R.id.check:
                goActivity(2);
                break;
            case R.id.delete:
                delete();
                break;
            case R.id.update:
                goActivity(3);
                break;
        }
    }

    private void goActivity(int type) {
        String id = etFaceId.getText().toString();
        if (id == null || id.equals("")) {
            showToast("请输入人脸识别唯一ID！");
            return;
        }
        Intent it = null;
        switch (type) {
            case 1:
                it = new Intent(MainActivity.this, RegisterFaceActivity.class);
                break;
            case 2:
                it = new Intent(MainActivity.this, DetectionFaceActivity.class);
                break;
            case 3:
                it = new Intent(MainActivity.this, UpdateFaceActivity.class);
                break;
        }
        it.putExtra("id", id);
        startActivity(new Intent(it));
    }

    private void delete(){
        String id = etFaceId.getText().toString();
        if (id == null || id.equals("")) {
            showToast("请输入人脸识别唯一ID！");
            return;
        }
        SharedPreferencesUtil sp = new SharedPreferencesUtil(MainActivity.this);
        String face_token = (String) sp.getSharedPreference("token", "");
        //删除请求
        HttpUtils.deleteFace(id,"groupId",face_token,new FaceRequestCallback(){
            @Override
            public void onShowDialog() {
                super.onShowDialog();
                showLoading("正在删除人脸...");
            }

            @Override
            public void onSuccess(String msg) {
                super.onSuccess(msg);
                showToast(msg);
            }

            @Override
            public void onFail(String error) {
                super.onFail(error);
                showToast(error);
            }

            @Override
            public void onDismissDialog() {
                super.onDismissDialog();
                dissmissLoading();
            }
        });
    }
}